package com.lagou.edu.service.impl;

import com.lagou.edu.annotation.MiniAutoWired;
import com.lagou.edu.annotation.MiniComponent;
import com.lagou.edu.annotation.MiniService;
import com.lagou.edu.annotation.MiniTransactional;
import com.lagou.edu.dao.IAccountDao;
import com.lagou.edu.dao.impl.JdbcIAccountDaoImpl;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.ITransferService;
import com.lagou.edu.utils.TransactionManager;

import java.math.BigDecimal;
import java.sql.SQLException;

/**
 * @ClassName: TransferServiceImpl
 * @Auther: Jerry
 * @Date: 2020/6/29 11:16
 * @Desctiption: TODO
 * @Version: 1.0
 */
@MiniService("transferService")
@MiniTransactional
public class TransferServiceImpl implements ITransferService {

    @MiniAutoWired
    private IAccountDao accountDao;

    @Override
    @MiniTransactional
    public void transfer(String fromCardNo, String toCardNo, BigDecimal money) throws SQLException {

        // 获取付款人信息
        Account from = accountDao.getAccountByCardNo(fromCardNo);
        // 获取收款人信息
        Account to = accountDao.getAccountByCardNo(toCardNo);

        BigDecimal fromMoney = from.getMoney().subtract(money);
        BigDecimal toMoney = to.getMoney().add(money);

        from.setMoney(fromMoney);
        to.setMoney(toMoney);

        // 更新金额
        accountDao.updateAccountByCardNo(from);
        int i = 1 / 0;
        accountDao.updateAccountByCardNo(to);

    }
}
